@extends('layouts.layouts')
@section('title')
    <title>后台首页</title>
@endsection
@section('content')
    <link rel="stylesheet" href="/lib/layer/theme/default/layer.css"/>
    <script type="text/javascript" src="/lib/layer/layer.js"></script>
    <div class="content">
        <div class="header">
            <ul class="breadcrumb">
                <li><a href="/courseLearn/card">活动列表</a></li>
                <li class="active" style="color:red;">{{$msg ? $msg : " "}}</li>
            </ul>
        </div>
        <div class="main-content">
            <div class="btn-toolbar list-toolbar">
                <form class="form-inline" action="" method="get">
                    <a href="/course/card/create"><button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal" ><i class="fa fa-plus"></i> 新增</button></a>
                    <div class="form-group">
                        {{--<input type="text" class="form-control" name="title" placeholder="课程标题" value="">--}}
                    </div>
                    {{--<button type="submit" class="btn btn-default">筛选</button>--}}
                </form>
            </div>
            <div class="row">
                <div class="col-sm-12 col-md-12">
                    <div class="panel panel-default">
                        <div class="panel-heading no-collapse">活动列表</div>
                        <table class="table table-bordered table-striped">
                            <thead>
                            <tr>
                                <th>编号</th>
                                <th>活动标题</th>
                                <th>活动课程类别</th>
                                <th>有效观看时间</th>
                                <th>开始时间</th>
                                <th>结束时间</th>
                                <th>操作</th>
                            </tr>
                            </thead>
                            <tbody>
                                @foreach($cards as $card)
                                <tr>
                                    <td>{{$card->id}}</td>
                                    <td>{{$card->name}}</td>
                                    <td>{{$card->courseType->title}}</td>
                                    <td>{{$card->validate_time}}天</td>
                                    <td>{{date('Y-m-d',strtotime($card->start))}}</td>
                                    <td>{{date('Y-m-d',strtotime($card->end))}}</td>
                                    <td class="text-justify">
                                        <a class="btn btn-success btn-xs" href="/course/card/edit/{{$card->id}}">编辑</a>
                                        <button class="btn btn-info btn-xs" data-id="{{$card->id}}" data-state="{{$card->status}}" onclick="courseCardState(this)">{{$card->status==1?'下架':'上架'}}</button>
                                        <a class="btn btn-primary btn-xs" href="/course/card/record/{{$card->id}}">活动记录</a>
                                    </td>
                                </tr>
                                @endforeach
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
            {{$cards->render()}}
        </div>
    </div>
    <script type="text/javascript">


        function courseCardState(obj){
            var id = $(obj).attr('data-id');
            var state = $(obj).attr('data-state');
            if(state == 1){
                var notice = '确定下架吗？';
                var newState = 0;
                var name = '上架'
            }else{
                var notice = '确定上架吗？';
                var newState = 1;
                var name = '下架';
            }
            var _token = '{{csrf_token()}}';
            var data = {id:id,state:newState,_token:_token};
            var object = obj;
            layer.confirm(notice, {
                btn: ['是', '否'],
            }, function(index){
                $.ajax({
                    url:'/course/card/updateState',
                    data:data,
                    type:'POST',
                    dataType: 'json',
                    success:function(res){
                        layer.alert(res.message);
                        if(res.code == 0){
                            $(object).attr('data-state',newState);
                            $(object).text(name);
                        }

                    }
                });
                layer.close(index);
                return true;
            }, function(index){
                layer.close(index);
            });
        }
        function courseDelete(obj){
            var id = $(obj).attr('data-id');
            var _token = '{{csrf_token()}}';
            var data = {id:id,_token:_token};
            layer.confirm('确定删除吗？', {
                btn: ['是', '否'],
            }, function(index){
                $.ajax({
                    url:'/courseClass/delete',
                    data:data,
                    type:'POST',
                    dataType: 'json',
                    success:function(res){
                        layer.alert(res.message);
                        if(res.code == 0){
                            $("#courseClass_"+id).remove();
                        }

                    }
                });
                layer.close(index);
                return true;
            }, function(index){
                layer.close(index);
            });

        }

    </script>
@endsection